﻿using Entity;
using Entity.Enum;
using Entity.ResultModel.RoomInfo;
using Entity.Room;
using Hotel.Models;
using IHotelBLL.IRoomBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Hotel.Controllers.Room.Info
{
    /// <summary>
    /// 房间控制器
    /// </summary>
    public class RoomInfoController : Controller
    {
        ResultModel res = new ResultModel();
        IRoomInfoBLL _roomInfoBLL;
        IRoomTypeBLL _roomTypeBLL;
        public RoomInfoController(IRoomInfoBLL roomInfoBLL, IRoomTypeBLL roomTypeBLL)
        {
            _roomInfoBLL = roomInfoBLL;
            _roomTypeBLL = roomTypeBLL;
        }

        // 列表数据接口
        public IActionResult GetRoomInfos(RoomStatusEnum status, int page, int limit, string name)
        {

            int count;
            List<GetRoomInfo> roomInfos = _roomInfoBLL.GetRoomInfos(status, page, limit, name, out count);

            var res = new
            {
                code = 0,
                msg = "",
                count,
                data = roomInfos

            };

            return Json(res);

        }

        //添加房间接口
        public IActionResult CreateRoomInfo(string name, string typeId, decimal price, string imageName)
        {
            //参数验证
            if (string.IsNullOrEmpty(name))
            {
                res.Msg = "客房名称不能为空";
                return Json(res);
            }
            if (string.IsNullOrEmpty(typeId))
            {
                res.Msg = "客房种类不能为空";
                return Json(res);
            }
            string msg;
            //创建
            bool isSuccess = _roomInfoBLL.CreateRoomInfo(name, typeId, price, imageName, out msg);
            if (isSuccess)
            {
                res.Status = 1;
            }
            res.IsSuccess = isSuccess;
            res.Msg = msg;
            return Json(res);
        }

        //更新接口
        public IActionResult UpdateRoomInfo(string id, string name, RoomStatusEnum status, string typeId, decimal price, string imageName)
        {
            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }
            if (string.IsNullOrEmpty(name))
            {
                res.Msg = "客房名称不能为空";
                return Json(res);
            }
            //调用bll修改业务方法
            bool isSuccess = _roomInfoBLL.UpdateRoomInfo(id, name, typeId, price, status, imageName, out string msg);

            if (isSuccess)
            {
                res.Status = 1;
                res.IsSuccess = true;
            }
            res.Msg = msg;
            return Json(res);
        }


        /// 删除
        [HttpPost]
        public IActionResult DeleteRoomInfo(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }
            //删除业务
            bool isSuccess = _roomInfoBLL.DeleteRoomInfo(id, out string msg);
            res.Msg = msg;
            res.IsSuccess = isSuccess;
            if (isSuccess)
            {
                res.Status = 1;
            }
            return Json(res);
        }
        public IActionResult GetRoomtypeId()
        {
            res.Data = _roomInfoBLL.GetRoomInfos();
            return Json(res);
        }
        // 批量删除
        [HttpPost]
        public IActionResult BatchDeleteRoomInfo(List<string> ids)
        {
            if (ids.Count == 0)
            {
                res.Msg = "请选择删除的房间";
                return Json(res);
            }
            //删除业务
            _roomInfoBLL.BatchDeleteRoomInfo(ids);

            res.Status = 1;
            res.IsSuccess = true;
            res.Msg = "删除成功";

            return Json(res);
        }
        public IActionResult GetRoomInfo(string TypeId)
        {
            res.Data = _roomInfoBLL.GetRoomInfos(TypeId);
            return Json(res);
        }
        // 根据id查询信息
        public IActionResult GetRoomInfoById(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                res.Msg = "id不能为空";
                return Json(res);
            }
            //根据id查信息
            RoomInfo roomInfo = _roomInfoBLL.GetRoomInfoById(id);
            if (roomInfo == null)
            {
                res.Msg = "未查询到客房信息";
                return Json(res);
            }
            //获取类别备选项数据集合
            List<SelectOptionModel> options = _roomTypeBLL.GetRoomTypeOptions();
            res.Msg = "成功";
            res.Status = 1;
            res.Data = roomInfo;
            res.Data = options;
            res.Data = new
            {
                RoomInfo = roomInfo,
                SelectOptionModel = options
            };
            res.IsSuccess = true;
            return Json(res);

        }

        //类别下拉选
        public IActionResult GetRoomTypeOptions()
        {
            res.Data = _roomTypeBLL.GetRoomTypeOptions();
            res.Status = 1;
            res.IsSuccess = true;
            if (res.Data is null)
            {
                res.Msg = "未找到类型";
                res.IsSuccess = false;
            }

            return Json(res);
        }
        public IActionResult GetRoomName(string typeId)
        {
            res.Data = _roomInfoBLL.GetRoomNameOptions(typeId);
            return Json(res);

        }
        //客房图片接口
        public IActionResult RoomImg(List<IFormFile> files)
        {
            //返回对象
            ResultModel res = new ResultModel();

            if (files.Count == 0)
            {
                res.Msg = "上传文件为空";
                return Json(res);
            }
            Stream stream = files[0].OpenReadStream();

            string fileName = files[0].FileName;
            if (fileName.Contains(@"\"))
            {
                fileName = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
            }

            string msg;
            //文件信息id
            string fileInfoId;
            bool isSuccess = _roomInfoBLL.RoomImg(fileName, stream, out fileInfoId, out msg);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Status = 1;
                res.Data = fileInfoId;
            }
            res.Msg = msg;
            return Json(res);
        }

    }
}



